<?php

namespace App\Console\Commands;

use App\Models\ParkInfo;
use App\Models\PayFee;
use Carbon\Carbon;
use Illuminate\Console\Command;
use App\Models\ParkRecord;
use App\Models\Report;

/**
 * 收费员统计报表
 *
 * Class ChargerReport
 * @package App\Console\Commands
 */
class ChargerReportFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'charger:day-reporter-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收费员日报表-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start       = Carbon::yesterday()->timestamp;
        $end         = Carbon::today()->timestamp;
        // $start       = mktime(0, 0, 0, 12, 24, 2018);
        // $end         = mktime(0, 0, 0, 12, 25, 2018);
        $report_date = date('Y-m-d', $start);


        $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
            ->select(['park_code', 'parking_name'])
            ->orderBy('sort_by', 'desc')
            ->get();

        $parks_code = config('custom.report_parks');
        $sort_num = 0;
        foreach ($parks_code as $val) {
            foreach ($parks as &$p) {
                if($p->park_code == $val){
                    $p->sort_num = $sort_num;
                    $sort_num++;
                }
            }
        }
        $parks = $parks->sortBy('sort_num');

        $data = $parks->map(function ($item, $key) use ($start, $end) {
            //查询前一天数据
            $chargerReportList=PayFee::where('park_code',$item->park_code)
                ->select(['charger',\DB::raw('IFNULL(SUM(pay_amount)/100,0) as charger_money'),'park_code'])
                ->where('park_code',$item->park_code)
                ->where('pay_time','>=',$start)
                ->where('pay_time','<',$end)
                ->where('pay_type',1)
                ->groupBy('charger')
                ->get();

            $chargerReportList = $chargerReportList->map(function($item) use ($start, $end){
                $row=$item->toArray();

                $row['hand_free']=PayFee::where('park_code',$item->park_code)
                    ->where('park_code',$item->park_code)
                    ->where('pay_time','>=',$start)
                    ->where('pay_time','<',$end)
                    ->where('charger',$item->charger)
                    ->where('pay_type',6)
                    ->count();
                return $row;
            });

            //车场收费员信息组合
            $parkRow=[];
            $parkRow['parking_name'] = $item->parking_name;
            $parkRow['chargeList']=array_values($chargerReportList->toArray());
            $parkRow['little_total']=$chargerReportList->sum('charger_money');
            $parkRow['little_total_free']=$chargerReportList->sum('hand_free');

            return $parkRow;
        });

        //计算合计
        $response = [];
        $response['total']=$data->sum('little_total');
        $response['total_free']=$data->sum('total_free');
        $response['data']=$data->toArray();


        //存入数据库
        $report = Report::where('type', 'charger_count_day_report')
                            ->where('report_date', $report_date)
                            ->first();
        if (!$report) {
            $report = new Report;
            $report->report_date = $report_date;
            $report->type = 'charger_count_day_report';
        }
        
        $report->value = $response;
        $report->save();


        $this->info('收费员日报计算成功');
    }
}
